#include"Linked.h"
#include<assert.h>
#include<stdlib.h>
#include<stdio.h>
void LinkedListPushBack(SLT** phead,SLTDateType x)
{
	assert(phead);
	SLT* newNode=STLNewNode(x);
	if (*phead == NULL)
	{
		*phead = newNode;
		return;
	 }
	SLT* tail = *phead;
	while (tail->next != NULL) {
		tail = tail->next;
	}
	tail->next = newNode;

}
SLT* STLNewNode(SLTDateType x)
{
	SLT* newNode = (SLT*)malloc(sizeof(SLT));
	if (newNode == NULL)
	{
		perror("malloc fail:");
		return;
	}
	newNode->value = x;
	newNode->next = NULL;
	return newNode;
}
void LinkedListDraw(SLT** pphead)
{
	assert(pphead);
	SLT* tail = *pphead;
	while (tail != NULL)
	{
		printf("%d->", tail->value);
		tail = tail->next;
	}
	printf("NULL\n");

}
void LinkedListPushFront(SLT** phead, SLTDateType x)
{
	assert(phead);
	SLT* Node=STLNewNode(x);
	Node->next = *phead;
	*phead = Node;
}
int LinkedSize(SLT** pphead)
{
	assert(pphead);
	SLT* tail = *pphead;
	int count = 0;
	while (tail != NULL) {
		count++;
		tail = tail->next;
	}
	return count;
}
void LinkedListPushInsert(SLT** phead,int pos, SLTDateType x)
{
	assert(phead);
	SLT* Node =STLNewNode(x);

	if (pos<0 || pos>LinkedSize(phead))
	{
		return;
	}
	SLT* prev = NULL;
	SLT* cur = *phead;
	while (pos-- > 0)
	{
		prev = cur;
		cur = cur->next;
	}
	Node->next = cur;	
	prev->next = Node;

}
void LinkedPopFront(SLT** pphead)
{
	assert(pphead);
	*pphead = (*pphead)->next;
}
void LinkedPopBack(SLT** pphead)
{
	assert(pphead);
	SLT* tail = *pphead;
	SLT* prev = NULL;
	while (tail->next != NULL)
	{
		prev = tail;
		tail = tail->next;
	}
	prev->next = NULL;
}
void LinkedPopInsert(SLT** pphead,int pos)
{
	assert(pphead);
	SLT* prev = NULL;
	SLT* cur = *pphead;
	while (pos-- > 0)
	{
		prev = cur;
		cur = cur->next;
	}
	prev->next = cur->next;
}